<template>
    <div class="container">
        <div class="header">
            <h1>支付结果</h1>
        </div>
        <div class="content">
  
          <div v-if="loading" class="loading">正在查询支付状态...</div>

            <div class="info-box" id="paymentDetails" >
            <h3>支付详情</h3>
            <p><strong>商户订单号:</strong> <span >{{ serialNumber }}</span></p>
            <p><strong>交易号:</strong> <span >{{ tradeNo }}</span></p>
            <p><strong>付款金额:</strong> <span >￥{{ cost }}</span></p>
            <p><strong>交易状态:</strong> <span >{{ statusCh }}</span></p>
            </div>

            <div id="actionButtons" class="action-buttons" >
            <button @click.prevent="toIndex()">返回首页</button>
            <button @click.prevent="toOrder()">查看订单</button>
            </div>
        </div>
    </div>
</template>

<script setup>
import axios from 'axios'
import {ref,onMounted,onUnmounted, nextTick} from 'vue'
import {useRouter,useRoute} from 'vue-router'
import store from '@/store'

const router = useRouter()
const route = useRoute()

const serialNumber = ref('')
const tradeNo = ref('')
const cost = ref('')
const status = ref('')
const statusCh = ref('')

 serialNumber.value = route.query.serialNumber || route.params.serialNumber || ''



function toIndex(){
  router.push({
        name: 'index',
    })
}
function toOrder(){
  router.push({
        path: '/allOrder',
    })
}

// 进入管理页面时隐藏HeaderView
onMounted(() => {
    store.commit('setShowPage', 1)

     serialNumber.value = route.query.serialNumber || route.params.serialNumber || ''
    console.log('接收到的订单号:', serialNumber.value)

    nextTick(() => {
    if(serialNumber.value!=''){
      getPaymentResult(serialNumber.value);
    }else{
      alert("参数获取失败");
    }
  })
})


// 离开管理页面时恢复HeaderView
onUnmounted(() => {
    store.commit('setShowPage', 0)
})

const getPaymentResult = async(serialNumber) => {
    try {
    const response = await axios.get('/api/orderReturn?serialNumber=' + serialNumber)
    console.log(response.data);
    
    tradeNo.value = response.data.tradeNo;
    cost.value = response.data.cost;
    status.value = response.data.status;

    if(status.value == 1){
      statusCh.value="未支付"
    }else if(status.value == 2){
      statusCh.value = "支付完成"
    }else if(status.value == 3){
      statusCh.value = "已超时"
    }else{
      statusCh.value = "已关闭"
    }
  } catch (error) { 
    alert("后端获取参数失败")
  }
}



</script>

<style scoped>
.container {
  min-height: 100vh;
  background-color: #f5f7fa;
  padding: 20px;
  box-sizing: border-box;
}

.header {
  text-align: center;
  padding: 30px 0;
  border-bottom: 1px solid #e5e7eb;
  margin-bottom: 30px;
}

.header h1 {
  font-size: 28px;
  color: #1e293b;
  margin: 0;
  font-weight: 600;
}

.content {
  max-width: 600px;
  margin: 0 auto;
  background-color: #ffffff;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
  padding: 30px;
  box-sizing: border-box;
}

.loading {
  text-align: center;
  padding: 50px 0;
  color: #64748b;
  font-size: 16px;
  animation: pulse 1.5s infinite;
}

@keyframes pulse {
  0%, 100% {
    opacity: 1;
  }
  50% {
    opacity: 0.6;
  }
}

.info-box {
  margin-bottom: 30px;
  border-radius: 8px;
  border: 1px solid #f1f5f9;
  padding: 25px;
  transition: all 0.3s ease;
}

.info-box:hover {
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.03);
  border-color: #e2e8f0;
}

.info-box h3 {
  margin-top: 0;
  margin-bottom: 20px;
  color: #334155;
  font-size: 20px;
  font-weight: 600;
  padding-bottom: 10px;
  border-bottom: 1px solid #f1f5f9;
}

.info-box p {
  margin: 12px 0;
  color: #64748b;
  font-size: 15px;
  line-height: 1.6;
}

.info-box p strong {
  color: #334155;
  margin-right: 8px;
}

.info-box p span {
  word-break: break-all;
}

/* 根据支付状态添加不同颜色 */
.info-box p span:nth-child(2):last-child {
  font-weight: 500;
}

.info-box p span:nth-child(2):last-child:contains("支付完成") {
  color: #10b981;
}

.info-box p span:nth-child(2):last-child:contains("未支付") {
  color: #f59e0b;
}

.info-box p span:nth-child(2):last-child:contains("已超时"),
.info-box p span:nth-child(2):last-child:contains("已关闭") {
  color: #ef4444;
}

.action-buttons {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-top: 40px;
}

.action-buttons button {
  padding: 12px 30px;
  border-radius: 6px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  border: none;
}

.action-buttons button:first-child {
  background-color: #ffffff;
  color: #3b82f6;
  border: 1px solid #3b82f6;
}

.action-buttons button:first-child:hover {
  background-color: #f0f9ff;
  color: #2563eb;
}

.action-buttons button:last-child {
  background-color: #3b82f6;
  color: #ffffff;
}

.action-buttons button:last-child:hover {
  background-color: #2563eb;
  transform: translateY(-2px);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .container {
    padding: 15px;
  }
  
  .header {
    padding: 20px 0;
  }
  
  .header h1 {
    font-size: 24px;
  }
  
  .content {
    padding: 20px;
  }
  
  .action-buttons {
    flex-direction: column;
    gap: 15px;
  }
  
  .action-buttons button {
    width: 100%;
    padding: 12px 0;
  }
}
</style>